﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EuroManager.Domain.Simulator.AI;
using EuroManager.Domain.Simulator.Engine;
using EuroManager.Domain.Simulator.Tactics;
using EuroManager.Domain.Simulator.World;
using Rhino.Mocks;

namespace EuroManager.Domain.Simulator.Tests.World
{
    public class FakeGameWorld : IGameWorld
    {
        private Team team1;
        private Team team2;
        private FakeRandomizer random = new FakeRandomizer();

        public FakeGameWorld()
            : this(Vector2D.Zero)
        {
        }

        public FakeGameWorld(Vector2D ballLocation)
        {
            Pitch = new Pitch();
            Ball = new Ball(Pitch, ballLocation);

            team1 = new Team(PitchSide.Left, Ball);
            team2 = new Team(PitchSide.Right, Ball);
            team1.Opponent = Team2;
            team2.Opponent = Team1;
        }

        public TimeSpan CurrentTime { get; set; }

        public bool IsFinished { get; set; }

        public IPitch Pitch { get; private set; }

        public IBall Ball { get; private set; }

        public ITeam Team1
        {
            get { return team1; }
        }

        public ITeam Team2
        {
            get { return team2; }
        }

        public IEnumerable<IPlayer> Players
        {
            get { return Enumerable.Concat(Team1.Players, Team2.Players); }
        }

        public void Update(float elapsedTime)
        {
            Ball.Update(elapsedTime);
            Team1.Update(elapsedTime);
            Team2.Update(elapsedTime);

            foreach (IPlayer player in Enumerable.Concat(Team1.Players, Team2.Players))
            {
                player.Update(elapsedTime);
            }
        }

        public Player AddPlayer()
        {
            return AddPlayer(team1);
        }

        public Player AddPlayer(Vector2D location)
        {
            return AddPlayer(team1, location);
        }

        public Player AddPlayer(ITeam team)
        {
            return AddPlayer(team, Vector2D.Zero);
        }

        public Player AddPlayer(IPlayerTactics tactics, Vector2D location)
        {
            Player player = new Player(4, Team1, Team2, tactics, Pitch, Ball, random, location);
            team1.AddPlayer(player);

            player.AI = MockRepository.GenerateStub<IPlayerAI>();

            return player;
        }

        public Player AddPlayer(ITeam team, Vector2D location)
        {
            Tactics442 tactics = new Tactics442();
            Player player = new Player(4, team, team.Opponent, tactics.GetPlayerTactics(4), Pitch, Ball, random, location);
            (team as Team).AddPlayer(player);

            player.AI = MockRepository.GenerateStub<IPlayerAI>();

            return player;
        }
    }
}
